﻿using QInfrastructure.Interfaces.SystemManager;
using QInfrastructure.Models.SystemManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QInfrastructure.Services.SystemManager;



public class SystemMeunService:QBaseMainService,TSystemMenu
{

    public void AddOrUpdateSystemMenu(SystemMenu menu)
	{
        using var db=CreateContext();
        if(menu.IsNew)
        {
            var tempId = db.Set<SystemMenu>().Select(x => x.Id).Max();
            menu.Id = tempId+1;
            db.Set<SystemMenu>().Add(menu);
        }
        else
        {
            db.Update(menu);
        }

        db.SaveChanges();
	}

    public SystemMenu GetSystemMenuById(int id)
    {
        using var db=CreateContext();
        var info=db.Set<SystemMenu>().FirstOrDefault(x => x.Id == id);
        return info;
    }
    public void DeleteSystemMenu(int id)
    {
        using var db = CreateContext();
        var info = db.Set<SystemMenu>().FirstOrDefault(x => x.Id == id);
        if(info != null)
        {
            db.Remove(info);
        }
        db.SaveChanges();

    }
    public List<SystemMenu> GetSystemMenuQuery(SystemMenu sysMenu)
	{
        var res = new List<SystemMenu>();
        using var db = CreateContext();
        var allmenus = db.Set<SystemMenu>().Where(x => !x.IsDelete).ToList();
        var needs = allmenus;
        if (!string.IsNullOrWhiteSpace(sysMenu.text)) needs = allmenus.Where(x => x.text.Contains(sysMenu.text)).ToList();
        var okmenus = new List<SystemMenu>();
        var okHashSet = new HashSet<int>();
        needs.ForEach(x =>
        {
            if (!okHashSet.Contains(x.Id))
            {
                okmenus.Add(x);
                okHashSet.Add(x.Id);
            }

            GetParents(x);
        });

        var roots = okmenus.Where(x => x.parentId == 0).ToList();
        roots.ForEach(x => GetChilds(x));
        res.AddRange(roots);

        void GetParents(SystemMenu info)
        {
            var pp = allmenus.Where(x => x.Id == info.parentId).FirstOrDefault();
            if (pp == null) return;
            if (!okHashSet.Contains(pp.Id))
            {
                okHashSet.Add(pp.Id);
                okmenus.Add(pp);
            }
            GetParents(pp);

        }

        void GetChilds(SystemMenu info)
        {
            var firstChilds = okmenus.Where(x => x.parentId == info.Id).ToList();
            if (firstChilds.Count <= 0)
            {
                info.state = "open";
                return;
            }
            firstChilds.ForEach(x => GetChilds(x));
            info.children = firstChilds;
        }

        return res;
    }
    public List<SystemMenu> GetSystemMenuList()
	{
        var db = CreateContext();
        var allmenus = db.Set<SystemMenu>().Where(x => !x.IsDelete).ToList();
        return allmenus;
    }


    public List<SystemMenu> GetSystemMenuTree(Guid UserId)
	{
		var db = CreateContext();
		var allmenus = db.Set<SystemMenu>().Where(x => !x.IsDelete).ToList();
		var root = allmenus.Where(x => x.parentId == 0).ToList();
		root.ForEach(x => GetChilds(x));
		return root;
		void GetChilds(SystemMenu syemenu)
		{
			var childs = allmenus.Where(x => x.parentId == syemenu.Id && x.Id!=x.parentId).ToList();
			if (childs.Count <= 0)
			{
				syemenu.state = "open";
				return;
			}
			childs.ForEach(x => GetChilds(x));
			syemenu.children = childs;
		}
	}
}
